package main

import (
	"fmt"
	"strconv"
	"sync"
	"time"
)

func main() {
	var mutex sync.Mutex
	cond := sync.NewCond(mutex)
	var condition = false
	go func() {
		for {
			cond.L.Lock()
			condition = !condition
			fmt.Println("GOROUTINE : condition is changing : " + strconv.FormatBool(condition))
			fmt.Println()
			cond.Signal()
			cond.L.Unlock()
			time.Sleep(time.Second * 3)
		}
	}()
	go func() {
		for {
			cond.L.Lock()
			fmt.Println("我插进来了\n")
			cond.Wait()
			cond.L.Unlock()
			time.Sleep(time.Second * 3)
		}
	}()
	for {
		cond.L.Lock()
		fmt.Println("MAIN : condition changed to " + strconv.FormatBool(condition))
		cond.Wait()
		fmt.Println()
		cond.L.Unlock()
		time.Sleep(time.Second * 3)
	}
}
